﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;


namespace Assignment1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        GuestBookDataContext ctx = new GuestBookDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SetBind();
            }
        }
        private void SetBind()
        {
            rpt_Message.DataSource = from gb in ctx.GuestBook orderby gb.PostTime descending select gb;
            rpt_Message.DataBind();
         }
    protected void rpt_Message_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "DeleteMessage")
        {
            StreamWriter sw = new StreamWriter(Server.MapPath("log.txt"), true);
            ctx.Log = sw;
            GuestBook gb = ctx.GuestBook.Single(b => b.ID == new Guid(e.CommandArgument.ToString()));
            ctx.GuestBook.DeleteOnSubmit(gb);
            ctx.SubmitChanges();
            SetBind();
            sw.Close();
        }
        if (e.CommandName == "SendReply")
        {
            StreamWriter sw = new StreamWriter(Server.MapPath("log.txt"), true);
            ctx.Log = sw;
            GuestBook gb = ctx.GuestBook.Single(b => b.ID == new Guid(e.CommandArgument.ToString()));
            gb.Reply = ((TextBox)e.Item.FindControl("tb_Reply")).Text;
            gb.IsReplied = true;
            ctx.SubmitChanges();
            SetBind();
            sw.Close();
        }
    }

 

        }

    }
